package com.scommunity.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scommunity.domain.AliPayBean;
import com.scommunity.domain.Pay;
import com.scommunity.mapper.PayMapper;
import com.scommunity.service.IPayService;
import com.scommunity.utils.Alipay;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PayServiceImpl extends ServiceImpl<PayMapper,Pay> implements IPayService{
    @Autowired
    Alipay alipay;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    PayMapper payMapper;
    @SneakyThrows
    @Override
    public String toPay(Pay pay) {
        //对pay做补全处理
        String orderName = null;
        if (pay.getPayType().equals("1")){
            orderName="物业费";
//            rabbitTemplate.convertAndSend("orderExchange","orderKey",pay);
//            rabbitTemplate.convertAndSend("delayExchange","delayKey",pay,msg->{
//                msg.getMessageProperties().setDelay(60*1000*5);
//                return msg;
//            });
        }else if (pay.getPayType().equals("2")){
            orderName= "水电费";
        }else if (pay.getPayType().equals("3")){
            orderName= "停车费";
        }
        //后续可以加其他类别的费用
        AliPayBean aliPayBean = new AliPayBean();
        //设置AlipayBean
        aliPayBean.setOut_trade_no(pay.getPayNum());
        aliPayBean.setSubject(orderName);
        aliPayBean.setTotal_amount(pay.getPayMoney().toString());
        aliPayBean.setBody(pay.getPayMark());
        String result = alipay.pay(aliPayBean);
        //逻辑是根据支付信息

        //怎么加上这个延迟队列呢
        return result;
    }
    //查看所有pay且分页
    @Override
    public IPage<Pay> findPage(Integer pageNum, Integer pageSize) {
        IPage<Pay> page=new Page<>(pageNum,pageSize);
        List<Pay> list = payMapper.findAll();
        page.setRecords(list);
        return page;
    }

    @Override
    public PageInfo<Pay> findPageByCondition(Integer pageNum, Integer pageSize, Pay pay) {
        //还是需要展示业主姓名，所以还是得关联查询
       // IPage<Pay> page=new Page<>(pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<Pay> list = payMapper.findByCondition(pay);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
